package hsmoto.supportClass
{
	import flash.display.Bitmap;
	import flash.display.BitmapData;
	import flash.display.MovieClip;
	import flash.events.Event;
	
	import hsmoto.UIContainer.Group;
	/**
	 * 布局管理器，负责统一更新所有的相对布局属性 
	 * @author Zhang
	 * 
	 */	
	public class LayoutManager
	{
		private static const _this:LayoutManager = new LayoutManager();
		
		private var _needRefresh:Array;
		private var _timer:MovieClip;
		
		public function LayoutManager()
		{
			if(_this)
			{
				throw new Error("当前类无需手动实例化");
				return;
			}
			_needRefresh = new Array();
			_timer = new MovieClip();
			_timer.addChild(new Bitmap(new BitmapData(1, 1)));//你看到它会想用啥用呢，因为_timer很容易被垃圾回收
			_timer.addEventListener(Event.EXIT_FRAME, onTime);
		}
		
		private function onTime(e:Event=null):void
		{
			_needRefresh.sortOn("depth", Array.NUMERIC | Array.DESCENDING);
			for each(var ui:Group in _needRefresh)
			{
				ui.updateDisplayList();
			}
			_needRefresh.length = 0;
		}
		/**
		 *立即刷新布局
		 */		
		public static function refresh():void
		{
			_this.onTime();
		}
		/**
		 * 添加到更新列表 
		 * @param ui 需要更新的对象
		 * @return 返回真说明添加成功
		 */		
		public static function addToUpdateList(ui:Group):Boolean
		{
			var notInList:Boolean = _this._needRefresh.indexOf(ui) == -1;
			if(notInList)
				_this._needRefresh.push(ui);
			return notInList;
		}
	}
}